package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.MaterialTypeEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.material.MaterialLibraryListReq;
import com.sikaryofficial.backend.domain.dto.req.material.MaterialLibraryReq;
import com.sikaryofficial.backend.domain.dto.resp.materical.MaterialLibraryDTO;
import com.sikaryofficial.backend.manager.MaterialLibraryManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 素材Controller
 *
 * @author qinjinyuan
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/materialLibrary/backend")
@Api(tags = "V3.5.1-素材库后台管理")
@Slf4j
public class MaterialController extends BaseController {
    @Autowired
    private MaterialLibraryManager materialLibraryManager;

    /**
     * 查询素材库分页列表
     */
    @PostMapping("/listPage")
    @ApiOperation("查询素材库分页列表")
    @TenantFilter
    public PageResult<MaterialLibraryDTO> listPage(@Validated @RequestBody MaterialLibraryListReq req) {
        // 查询获奖列表
        IPage<MaterialLibraryDTO> page = materialLibraryManager.listPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 后台管理-素材类型
     */
    @PostMapping("/getMaterialType")
    @ApiOperation("素材分类")
    public R<List<SysDictDataDTO>> getMaterialType() {
        MaterialTypeEnum[] reportTypeEnums = MaterialTypeEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (MaterialTypeEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictLabel(reportTypeEnum.getName() + "");
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode() + "");
            sysDictDataDTO.setDictType("material_type");
        }
        return R.ok(result);
    }

    /**
     * 素材库详细信息
     */
    @GetMapping(value = "/detail/{materialId}")
    @ApiOperation("素材库详细信息")
    @TenantFilter
    public R<MaterialLibraryDTO> getInfo(@PathVariable("materialId") Long materialId) {
        return R.ok(materialLibraryManager.getInfo(materialId));
    }

    /**
     * 素材库新增或修改
     */
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation("素材库新增或修改")
    @TenantFilter
    public R<Long> saveOrUpdate(@Validated @RequestBody MaterialLibraryReq req) {
        return R.ok(materialLibraryManager.saveOrUpdate(req));
    }

    /**
     * 删除素材库
     */
    @Log(title = "素材库", businessType = BusinessType.DELETE)
    @GetMapping(value = "/remove/{materialIds}")
    @ApiOperation("删除素材库")
    @TenantFilter
    public R<Boolean> remove(
            @ApiParam(value = "materialIds，支持一个或多个,英文逗号分隔", required = true)
            @PathVariable String materialIds) {
        materialLibraryManager.remove(materialIds);
        return R.ok(Boolean.TRUE);
    }
}
